﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.WSA;

public class PlantGrow : MonoBehaviour
{
    public Sprite[] grow_img;//生长状态
    private SpriteRenderer spriteRenderer;
    private int grow_count;
    private bool can_gain;//可以收获
    public float grow_time;
    private float current_time;
    public GameObject seed;
    void Start()
    {
        current_time = grow_time;
        spriteRenderer = GetComponent<SpriteRenderer>();
    }
    private void Update()
    {
        if(current_time > 0 && grow_count < 3)
        {
            current_time -= Time.deltaTime;
        }
        else if(current_time <= 0 && grow_count < 3)
        {
            Grow();
        }
        if(grow_count == 3)//成熟
        {
            can_gain = true;
        }
    }
    void Grow()//生长
    {
        current_time = grow_time;
        grow_count++;
        spriteRenderer.sprite = grow_img[grow_count];
    }

    private void OnTriggerStay2D(Collider2D other) 
    {
        if (other.gameObject.name == "Player")
        {
            if(Input.GetKeyDown(KeyCode.Space) && can_gain)
            {
                Destroy(this.gameObject);
                other.GetComponent<Player>().canPlant = true;
                Instantiate(seed, this.transform.position, Quaternion.identity);
                Instantiate(seed, this.transform.position, Quaternion.identity);
                Instantiate(seed, this.transform.position, Quaternion.identity);
            }
        }
    }
    

}
